package com.uphone.magicleafteacher.pro.fragment.home;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.radish.baselibrary.Intent.IntentUtils;
import com.radish.baselibrary.navigationbar.DefaultNavigationBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.uphone.magicleafteacher.R;
import com.uphone.magicleafteacher.base.mvp.BaseMvpFragment;
import com.uphone.magicleafteacher.pro.activity.AdviserActivity;
import com.uphone.magicleafteacher.pro.activity.BookBorrowingActivity;
import com.uphone.magicleafteacher.pro.activity.CourseLiveListActivity;
import com.uphone.magicleafteacher.pro.activity.CoursedetailsActivity;
import com.uphone.magicleafteacher.pro.activity.FinishWorkActivity;
import com.uphone.magicleafteacher.pro.activity.MorecourseActivity;
import com.uphone.magicleafteacher.pro.activity.guidancetest.GuidancetestActivity;
import com.uphone.magicleafteacher.pro.activity.listenertrainlist.ListenerTrainListActivity;
import com.uphone.magicleafteacher.pro.activity.retellinglearn.RetellingLearnActivity;
import com.uphone.magicleafteacher.pro.activity.startstudent.StartStudentActivity;
import com.uphone.magicleafteacher.pro.adapter.CoursesAdapter;
import com.uphone.magicleafteacher.pro.adapter.HomefunctionAdapter;
import com.uphone.magicleafteacher.util.CommonUtil;
import com.uphone.magicleafteacher.util.GsonUtils;
import com.uphone.magicleafteacher.vo.FuncitionBean;
import com.uphone.magicleafteacher.vo.OtherLiveClassBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 首页
 * <pre>  *     author : radish  *     e-mail : 15703379121@163.com  *     time   : 2019/4/16  *     desc   :  * </pre>
 */
public class HomeFragment extends BaseMvpFragment<HomePresenter> implements HomeContract.View {
    @BindView(R.id.home_funcition)
    RecyclerView rv_funcition;
    List<FuncitionBean> list_funcition = new ArrayList<FuncitionBean>();
    @BindView(R.id.rc_other_courses)
    RecyclerView rcOtherCourses;
    CoursesAdapter coursesAdapter;
    @BindView(R.id.tv_more_course)
    TextView tvMoreCourse;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.refreshLayout)
    protected SmartRefreshLayout refreshLayout;
    private HomefunctionAdapter mAdapter;

    public static HomeFragment newInstance(String title) {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        new DefaultNavigationBar.Builder(getActivity(), ll)
                .setLineColor(getResources().getColor(R.color.color_graye))
                .setTitle(CommonUtil.getStr("我的工作台"))
                .builder()
                .getLeftIcon()
                .setVisibility(View.GONE);
        rcOtherCourses.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        coursesAdapter = new CoursesAdapter();
        rcOtherCourses.setAdapter(coursesAdapter);
        showfunciton();

    }

    @Override
    protected void loadData() {
        mPresenter.loadNetData(1);
    }

    @Override
    protected void initListener() {
        coursesAdapter.setOnItemClickListener((adapter, view, position) -> {
            //点击条目
            CommonUtil.startIntent(getActivity(), CoursedetailsActivity.class, coursesAdapter.getItem(position).getLiveId());
        });
        coursesAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            switch (view.getId()) {
                case R.id.by_buy_course://购买课程
                    break;
            }
        });
        mAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            switch (position) {
                case 0:
                    //听力指导
                    IntentUtils.getInstance().with(getActivity(), ListenerTrainListActivity.class).start();
                    break;
                case 1:
                    //直播课程
                    IntentUtils.getInstance().with(getActivity(), CourseLiveListActivity.class).start();
                    break;
                case 2:
                    //作业批改
                    IntentUtils.getInstance().with(getActivity(), FinishWorkActivity.class).start();
                    break;
                case 3:
                    //跟读指导
                    IntentUtils.getInstance().with(getActivity(), BookBorrowingActivity.class).start();
                    break;
                case 4:
                    //阅读点评
                    IntentUtils.getInstance().with(getActivity(), RetellingLearnActivity.class).start();
                    break;
                case 5:
                    //复述批改
                    IntentUtils.getInstance().with(getActivity(), GuidancetestActivity.class).start();
                    break;
                case 6:
                    //指导检测
                    IntentUtils.getInstance().with(getActivity(), AdviserActivity.class).start();
                    break;
                case 7:
                    //明星学员
                    IntentUtils.getInstance().with(getActivity(), StartStudentActivity.class).start();
                    break;
            }
        });

        CommonUtil.initRefresh(getActivity(), refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                mPresenter.loadNetData(1);
                refreshlayout.finishRefresh(1500);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                mPresenter.loadNetData(mPresenter.page + 1);
                refreshlayout.finishLoadMore(1500);
            }
        });
    }

    @Override
    public void updateUI(String body) {
        OtherLiveClassBean otherLiveClassBean = GsonUtils.getGson().fromJson(body, OtherLiveClassBean.class);
        if (otherLiveClassBean != null) {
            if (mPresenter.page == 1) {
                coursesAdapter.setNewData(otherLiveClassBean.getLiveClassList());
            } else {
                coursesAdapter.addData(otherLiveClassBean.getLiveClassList());
            }
        } else if (mPresenter.page != 1) {
            mPresenter.page = mPresenter.page - 1;
            ToastUtils.showShort("暂无更多数据");
        }
    }

    private void showfunciton() {
        rv_funcition.setLayoutManager(new GridLayoutManager(getActivity(), 4));
        String[] name = {"听力指导", "直播课程", "作业批改", "跟读指导", "阅读点评"
                , "复述批改", "指导检测", "明星学员"};
        int[] image = {
                R.mipmap.home0, R.mipmap.home1, R.mipmap.home2,
                R.mipmap.home3, R.mipmap.home4, R.mipmap.home5, R.mipmap.home6, R.mipmap.home7
        };
        list_funcition.clear();
        for (int i = 0; i < image.length; i++) {
            FuncitionBean bean = new FuncitionBean();
            bean.setImage(image[i]);
            bean.setName(name[i]);
            list_funcition.add(bean);
        }
        mAdapter = new HomefunctionAdapter(list_funcition);
        rv_funcition.setAdapter(mAdapter);
    }

    @OnClick({R.id.tv_more_course})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_more_course:
                IntentUtils.getInstance().with(getActivity(), MorecourseActivity.class).start();
                break;
        }
    }
}
